#ifndef EVENTLOOP_EPOLLER_H
#define EVNETLOOP_EPOLLER_H

#include "Poller.h"

#include <map>


struct epoll_event;

class Epoller final: public Poller
{
public:
    explicit Epoller(Eventloop &loop);
    virtual ~Epoller() override;

    virtual bool has(const IOchannelProxy &channel) const override;

    /// @param timeout ms
    virtual Utility::timepoint poll(int timeout , IOchannelProxyRefs &activeChannels) override;
    virtual void remove(IOchannelProxy &channel) override;
    virtual void update(IOchannelProxy &channel) override;

private:
    NONCOPYABLE(Epoller);

    struct _Helper final {
        _Helper(): channel{}, alive{true} {}
        explicit _Helper(IOchannelProxy &channel_):
            channel{channel_}, alive{true} {}

        const OptionalIOchannelProxyRef channel;
        bool alive;
    };

    void _change(const IOchannelProxy &channel);
    void _fill_active_channels(
        int nfds, IOchannelProxyRefs &activeChannels) const override;
    bool _is_in_channels(int fd) const;
    void _update(const IOchannelProxy &channel, int operatioin) const;

    std::map<int, _Helper> _fdMap;
    std::vector<struct epoll_event> _events;
    const int _pollFD;
};



#endif